package com.jxm.controller;


import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.paragetter.Para;
import com.jfinal.plugin.activerecord.Page;
import com.jxm.model.User;
import com.jxm.service.UserService;
import com.jxm.validator.UserValidator;
import com.jxm.vo.IResult;

public class UserController extends BaseController {
	
	@Inject
	private UserService userService;
	
	/**
	 * 默认调用方法
	 */
	public void index() {
		renderText("Hello JFinal World.");
	}

	/**
	 * 渲染用户信息
	 */
	public void list() {
		renderJsp("list.jsp");
	}
	
	/**
	 * 获取用户信息列表
	 */
	public void dataList(){
		String username = this.getPara("username");
		Page<User> userPage = userService.getPage(this.getPage(), this.getLimit(),username);
		this.renderPageForLayUI(userPage);
	}
	
	public void add(){
		renderJsp("add.jsp");
	}
	
	/**
	 * 保存用户
	 */
	@Before(UserValidator.class)  
	public void save(@Para("sex")Integer sex,@Para("remark")String remark,
			@Para("username")String username,@Para("status")Integer status){
		IResult result =  userService.save(username,sex,status,remark);
		renderJson(result);
		
	}
	
	/**
	 * 根据id获取用户信息显示到修改页面
	 */
	public void edit(@Para("id")Integer id){
		User user = userService.findById(id);
		this.setAttr("user", user);
		renderJsp("edit.jsp");
	}
	
	/**
	 * 修改用户信息
	 */
	public void update(@Para("id")Integer id,@Para("sex")Integer sex,
			@Para("remark")String remark,@Para("status")Integer status){
		IResult result = userService.update(id,sex,remark,status);
		renderJson(result);
	}
	
	/**
	 * 根据id删除用户
	 * @param id
	 */
	public void delete(@Para("id")Integer id){
		renderJson(userService.delte(id));
	}
	
	/**
	 * 根据id修改用户状态-冻结
	 * @param id 
	 */
	public void lock(@Para("id")Integer id){
		renderJson(userService.lock(id));
	}
	
	/**
	 * 根据id修改用户状态-解封
	 * @param id unlock
	 */
	public void unlock(@Para("id")Integer id){
		renderJson(userService.unlock(id));
	}
}
